#!/bin/sh

## Script to run to ensure crosslight was built and installed correctly

cd crosslight

#Test files exist
[ -d bin ] && [ -f bin/lighttpd ] && [ -f etc/sites/enabled.d/default.conf ] || { echo "Failed: Could not find expected files" ; exit 1 ; }

#Test start
sh crosslight.sh start | wc -l | while read n ; do [ $n -eq 1 ] || { echo "Failed: incorrect start announcement" ; exit 2 ; } ; done
[ -f var/tmp/lighttpd.pid ] || { echo "Failed: lighttpd.pid not found." ; exit 3 ; }
ps -p $(cat var/tmp/lighttpd.pid) >/dev/null 2>&1 || { echo "Failed: lighttpd.pid is not an active process." ; exit 4 ; }

#Test static
	#Test /
	curl -v localhost:3000 >_ 2>&1
	fgrep '<h2>It Works!</h2>' _ >/dev/null || { echo "Failed: GET localhost:3000 did not return index." ; exit 5 ; }

	#Test index.html
	curl -v localhost:3000/index.html > index.html 2>&1
	fgrep '<h2>It Works!</h2>' index.html >/dev/null || { echo "Failed: GET localhost:3000/index.html did not return index." ; exit 6 ; }

	#Test favicon.ico
	curl -v localhost:3000/favicon.ico > favicon.ico 2>&1
	fgrep '< Content-Length: 1150' favicon.ico >/dev/null || { echo "Failed: GET localhost:3000/favicon.ico did not return entity of the correct size."; exit 7; }
	fgrep '< Cache-Control: public, max-age=604800' favicon.ico >/dev/null || { echo "Failed: GET localhost:3000/favicon.ico did not return correct Cache-Control."; exit 8; }

	#Test unknown subdomain
	curl -v bad.localhost:3000/ > bad.html 2>&1
	curl -v bad.localhost:3000/favicon.ico > bad.ico 2>&1
	fgrep '< HTTP/1.1 404 Not Found' bad.html >/dev/null || { echo "Failed: GET bad.localhost:3000 should 404." ; exit 9 ; }
	fgrep '< HTTP/1.1 404 Not Found' bad.ico >/dev/null || { echo "Failed: GET bad.localhost:3000/favicon.ico should 404." ; exit 10 ; }


#Test PHP
	#Test info.php
	curl -v localhost:3000/info.php > info.php 2>&1
	fgrep '< HTTP/1.1 200 OK' info.php >/dev/null || { echo "Failed: GET localhost:3000/info.php did not return successfully." ; exit 11 ; }
	fgrep '<title>phpinfo()</title>' info.php >/dev/null || { echo "Failed: GET localhost:3000/info.php did not return an info.php page." ; exit 12 ; }

#Test PHP Modules
	#Test SQLite3
	fgrep 'SQLite3 support</th><th>enabled' info.php >/dev/null || { echo "Failed: PHP SQLite3 module not enabled." ; exit 13 ; }

	#Test GD
	egrep 'GD.*enabled' info.php >/dev/null || { echo "Failed: PHP GD module not enabled." ; exit 14 ; }
	egrep 'GIF Read.*enabled' info.php >/dev/null || { echo "Failed: PHP GD has no GIF Read support." ; exit 15 ; }
	egrep 'GIF Create.*enabled' info.php >/dev/null || { echo "Failed: PHP GD has no GIF Create support." ; exit 16 ; }
	egrep 'JPEG.*enabled' info.php >/dev/null || { echo "Failed: PHP GD has no JPEG support." ; exit 17 ; }
	egrep 'PNG.*enabled' info.php >/dev/null || { echo "Failed: PHP GD has no PNG support." ; exit 18 ; }
	egrep 'MysqlI.*enabled' info.php >/dev/null || { echo "Warning: PHP has no MySQLI support." ; }
	egrep 'mysqlnd.*enabled' info.php >/dev/null || { echo "Warning: PHP has no MySQLND support." ; }

	#Test cURL
	egrep 'cURL.*enabled' info.php >/dev/null || { echo "Failed: PHP has no cURL support." ; exit 19 ; }

rm _ index.html info.php favicon.ico bad*

#Test stop
sh crosslight.sh stop | wc -l | while read n ; do [ $n -eq 1 ] || { echo "Failed: incorrect stop announcement" ; exit 20 ; } ; done

if [ ! $(which ncat) ]
then
	echo "The rest of the tests require ncat- attempting install."
	sudo yum install -y nmap >/dev/null
fi

#Test find port
ncat -l localhost 3000 & npid=$!
sh crosslight.sh start -p 3000 | fgrep ':3001' >/dev/null 2>&1 || { echo "Failed: Could not find next higher port." ; exit 21 ; }
sh crosslight.sh stop >/dev/null
kill $npid

	#Test debug

	#Test proxy
	#Load and start proxy
	[ -f etc/sites/sites.d/proxy.conf ] || { echo "Failed: no proxy to check." ; exit 22 ; }
	ln -s etc/sites/enabled.d/proxy.conf etc/sites/sites.d/proxy.conf
	ncat -e ../simpleproxy.sh -l 8080 & npid=$!

	sh crosslight.sh start
	curl -v proxy.localhost:3000 >proxy.html 2>&1
	fgrep '<h2>Proxied!</h2>' proxy >/dev/null ; PASS=$!
	sh crosslight.sh stop

	kill $npid
	rm etc/sites/enabled.d/proxy.conf
	rm proxy.html

	[ $PASS -eq 0 ] || { echo "Failed: Could not load from proxy." ; exit 23 }

echo "Tests passed."
